package com.terrier.ui.controls
{
    import com.terrier.ui.controls.scrollClasses.WxScrollBarBase;
    import com.terrier.ui.skins.WxSkinManager;

    import flash.geom.Point;

    /**
     * 水平滚动条
     * @author Jarvis.weng
     */
    public class WxHScrollBar extends WxScrollBarBase
    {
        /**
         * 构造函数
         */
        public function WxHScrollBar()
        {
            super();
        }

        /**
         * @private
         */
        override public function setSkinID(skinID:String):void
        {
            setSkin(WxSkinManager.getHScrollBarSkin(skinID));
        }

        /**
         * @private
         */
        override protected function updateLayout():void
        {
            super.updateLayout();

            if (!_skin.customLayout)
            {
                track.width = _width - increaseButton.width - decreaseButton.width;
                decreaseButton.x = 0;
                track.x = decreaseButton.width;
                increaseButton.x = track.x + track.width;
				
				track.height = decreaseButton.height = increaseButton.height = thumb.height = _height;

                if (pageSize == 0 || range == 0)
                {
                    thumb.visible = false;
                }
                else
                {
                    thumb.width = track.width * pageSize / (range + pageSize);
                    thumb.visible = true;
                }
            }

            if (!isDrag)
            {
                updateThumbPosition();
            }
        }

        /**
         * @private
         */
        override protected function dragThumb(pt:Point):void
        {
            var minPos:Number = track.x;
            var maxPos:Number = track.x + track.width - thumb.width;
            var posRange:Number = maxPos - minPos;
            var xPos:Number = Math.max(minPos, Math.min(pt.x, maxPos));

            thumb.x = xPos;

            value = (xPos - minPos) / posRange * range + minimum;
        }

        /**
         * @private
         */
        override protected function updateThumbPosition():void
        {
            var minPos:Number = track.x;
            var maxPos:Number = track.x + track.width - thumb.width;
            var posRange:Number = maxPos - minPos;

            thumb.x = percentValue * posRange + minPos;
        }

        /**
         * @private
         */
        override protected function pointToValue(pt:Point):Number
        {
            var minPos:Number = 0;
            var maxPos:Number = track.width - thumb.width;
            var posRange:Number = maxPos - minPos;
            var xPos:Number = pt.x;

            return (xPos - minPos) / posRange * range + minimum;
        }
    }
}
